
*=============================================================================

*** Setup

version 15
capture log close
set more off
clear all

* Load data
use "$data/us_prep_import.dta"

* Keep only if two-alternative start year version of conjoint
keep if CPCTC_treatment==1

*=============================================================================

*** Timing

/* Loop over all 8 tasks*/
forvalues i=1(1)8 {
* Scenarios 1 and 2
	forvalues j=1/2 {
* Own country timing
gen own_csttmng_cj`i'_s`j'=CPCT_V1_T0`i'_B1_S`j'_Year
replace own_csttmng_cj`i'_s`j'=. if CPCT_V1_T0`i'_B1_S`j'_Year==9999
label var own_csttmng_cj`i'_s`j' "Own country: contributions start year"

* Other countries timing
gen other_csttmng_cj`i'_s`j'=CPCT_V1_T0`i'_B2_S`j'_Year
replace other_csttmng_cj`i'_s`j'=. if CPCT_V1_T0`i'_B2_S`j'_Year==9999
label var other_csttmng_cj`i'_s`j' "Other country: contributions start year"
	}
}

*=============================================================================

*** Household costs

* 9 means not asked

/* Loop over all 8 tasks*/
forvalues i=1(1)8 {
* Scenarios 1 and 2
	forvalues j=1/2 {
	gen own_hhcsts_cj`i'_s`j'=CPCT_V1_T0`i'_B1_S`j'_Cost if CPCT_V1_T0`i'_B1_S`j'_Cost<9
	gen other_hhcsts_cj`i'_s`j'=CPCT_V1_T0`i'_B2_S`j'_Cost if CPCT_V1_T0`i'_B2_S`j'_Cost<9
	}
}


*=============================================================================

*** Adaptation and mitigation mix

* Continuous from 0 to 100
* 999 means not asked

/* Loop over all 8 tasks*/
forvalues i=1(1)8 {
* Loop over scenarios 1 and 2
	forvalues j=1/2 {
	* Own country
	gen own_adapt_cj`i'_s`j'=CPCT_V1_T0`i'_B1_S`j'_Adapt if CPCT_V1_T0`i'_B1_S`j'_Adapt!=999
	gen own_mitig_cj`i'_s`j'=CPCT_V1_T0`i'_B1_S`j'_Mitigate if CPCT_V1_T0`i'_B1_S`j'_Mitigate!=999

	* Other countries
	gen other_adapt_cj`i'_s`j'=CPCT_V1_T0`i'_B2_S`j'_Adapt if CPCT_V1_T0`i'_B2_S`j'_Adapt!=999
	gen other_mitig_cj`i'_s`j'=CPCT_V1_T0`i'_B2_S`j'_Mitigate if CPCT_V1_T0`i'_B2_S`j'_Mitigate!=999
	}
}

*=============================================================================

*** Outcome

forvalues i=1/8 {
* Loop over scenarios 1 and 2
	gen choice_cj`i'_s1=0 if CPCT_V1_T0`i'_RA1==2
	replace choice_cj`i'_s1=1 if CPCT_V1_T0`i'_RA1==1
	recode choice_cj`i'_s1 (0=1) (1=0), gen(choice_cj`i'_s2)
}

*=============================================================================

*** Reshape to conjoint level (wide to long)

* List all conjoint attribute level variables
local owncstpath="own_cstpth_cj1_s own_cstpth_cj2_s own_cstpth_cj3_s own_cstpth_cj4_s own_cstpth_cj5_s own_cstpth_cj6_s own_cstpth_cj7_s own_cstpth_cj8_s"
local othercstpath="other_cstpth_cj1_s other_cstpth_cj2_s other_cstpth_cj3_s other_cstpth_cj4_s other_cstpth_cj5_s other_cstpth_cj6_s other_cstpth_cj7_s other_cstpth_cj8_s" 
local owncsttmng="own_csttmng_cj1_s own_csttmng_cj2_s own_csttmng_cj3_s own_csttmng_cj4_s own_csttmng_cj5_s own_csttmng_cj6_s own_csttmng_cj7_s own_csttmng_cj8_s"
local othercsttmng="other_csttmng_cj1_s other_csttmng_cj2_s other_csttmng_cj3_s other_csttmng_cj4_s other_csttmng_cj5_s other_csttmng_cj6_s other_csttmng_cj7_s other_csttmng_cj8_s"
local ownhhcsts="own_hhcsts_cj1_s own_hhcsts_cj2_s own_hhcsts_cj3_s own_hhcsts_cj4_s own_hhcsts_cj5_s own_hhcsts_cj6_s own_hhcsts_cj7_s own_hhcsts_cj8_s"
local otherhhcsts="other_hhcsts_cj1_s other_hhcsts_cj2_s other_hhcsts_cj3_s other_hhcsts_cj4_s other_hhcsts_cj5_s other_hhcsts_cj6_s other_hhcsts_cj7_s other_hhcsts_cj8_s"
local ownadapt="own_adapt_cj1_s own_adapt_cj2_s own_adapt_cj3_s own_adapt_cj4_s own_adapt_cj5_s own_adapt_cj6_s own_adapt_cj7_s own_adapt_cj8_s"
local otheradapt="other_adapt_cj1_s other_adapt_cj2_s other_adapt_cj3_s other_adapt_cj4_s other_adapt_cj5_s other_adapt_cj6_s other_adapt_cj7_s other_adapt_cj8_s" 
local ownmitig="own_mitig_cj1_s own_mitig_cj2_s own_mitig_cj3_s own_mitig_cj4_s own_mitig_cj5_s own_mitig_cj6_s own_mitig_cj7_s own_mitig_cj8_s"
local othermitig="other_mitig_cj1_s other_mitig_cj2_s other_mitig_cj3_s other_mitig_cj4_s other_mitig_cj5_s other_mitig_cj6_s other_mitig_cj7_s other_mitig_cj8_s" 
local choice="choice_cj1_s choice_cj2_s choice_cj3_s choice_cj4_s choice_cj5_s choice_cj6_s choice_cj7_s choice_cj8_s"
local erating="effrting_cj1_s effrting_cj2_s effrting_cj3_s effrting_cj4_s effrting_cj5_s effrting_cj6_s effrting_cj7_s effrting_cj8_s"

reshape long `owncstpath' `othercstpath' `owncsttmng' `othercsttmng' `ownhhcsts' `otherhhcsts' `ownadapt' `otheradapt' `ownmitig' `othermitig' `choice' `erating', i(caseid) j(scenario)

*=============================================================================

*** Reshape to scenario level

* List all scenario attribute level variables

local owncstpath="own_cstpth_cj1 own_cstpth_cj2 own_cstpth_cj3 own_cstpth_cj4 own_cstpth_cj5 own_cstpth_cj6 own_cstpth_cj7 own_cstpth_cj8"
local othercstpath="other_cstpth_cj1 other_cstpth_cj2 other_cstpth_cj3 other_cstpth_cj4 other_cstpth_cj5 other_cstpth_cj6 other_cstpth_cj7 other_cstpth_cj8" 
local owncsttmng="own_csttmng_cj1 own_csttmng_cj2 own_csttmng_cj3 own_csttmng_cj4 own_csttmng_cj5 own_csttmng_cj6 own_csttmng_cj7 own_csttmng_cj8"
local othercsttmng="other_csttmng_cj1 other_csttmng_cj2 other_csttmng_cj3 other_csttmng_cj4 other_csttmng_cj5 other_csttmng_cj6 other_csttmng_cj7 other_csttmng_cj8"
local ownhhcsts="own_hhcsts_cj1 own_hhcsts_cj2 own_hhcsts_cj3 own_hhcsts_cj4 own_hhcsts_cj5 own_hhcsts_cj6 own_hhcsts_cj7 own_hhcsts_cj8"
local otherhhcsts="other_hhcsts_cj1 other_hhcsts_cj2 other_hhcsts_cj3 other_hhcsts_cj4 other_hhcsts_cj5 other_hhcsts_cj6 other_hhcsts_cj7 other_hhcsts_cj8"
local ownadapt="own_adapt_cj1 own_adapt_cj2 own_adapt_cj3 own_adapt_cj4 own_adapt_cj5 own_adapt_cj6 own_adapt_cj7 own_adapt_cj8"
local otheradapt="other_adapt_cj1 other_adapt_cj2 other_adapt_cj3 other_adapt_cj4 other_adapt_cj5 other_adapt_cj6 other_adapt_cj7 other_adapt_cj8" 
local ownmitig="own_mitig_cj1 own_mitig_cj2 own_mitig_cj3 own_mitig_cj4 own_mitig_cj5 own_mitig_cj6 own_mitig_cj7 own_mitig_cj8"
local othermitig="other_mitig_cj1 other_mitig_cj2 other_mitig_cj3 other_mitig_cj4 other_mitig_cj5 other_mitig_cj6 other_mitig_cj7 other_mitig_cj8" 
local choice="choice_cj1 choice_cj2 choice_cj3 choice_cj4 choice_cj5 choice_cj6 choice_cj7 choice_cj8"
local erating="effrting_cj1 effrting_cj2 effrting_cj3 effrting_cj4 effrting_cj5 effrting_cj6 effrting_cj7 effrting_cj8"

foreach stub in `owncstpath' `othercstpath' `owncsttmng' `othercsttmng' `ownhhcsts' `otherhhcsts' `ownadapt' `otheradapt' `ownmitig' `othermitig' `choice' `erating' {
rename `stub'_s `stub'
}
reshape long ///
own_cstpth_cj other_cstpth_cj ///
own_csttmng_cj other_csttmng_cj ///
own_hhcsts_cj other_hhcsts_cj ///
own_adapt_cj other_adapt_cj ///
own_mitig_cj other_mitig_cj ///
choice_cj effrting_cj ///
, i(caseid scenario) j(conjoint)

*=============================================================================

*** Generate common variables

* Household costs
foreach pre in own other {
tab `pre'_hhcsts_cj, gen(`pre'_hhcsts_)
rename `pre'_hhcsts_1 `pre'_hhcsts_low
rename `pre'_hhcsts_2 `pre'_hhcsts_med
rename `pre'_hhcsts_3 `pre'_hhcsts_high
rename `pre'_hhcsts_4 `pre'_hhcsts_vhigh
}

* Two-year buckets for start year
* Bins of 2
lab define tmng_10levels_lab 1 "2020-21" 2 "2022-23" 3 "2024-25" 4 "2026-27" ///
5 "2028-29" 6 "2030-31" 7 "2032-33" 8 "2034-35" 9 "2036-37" 10 "2038-40"
foreach var in own_csttmng_cj other_csttmng_cj {
gen `var'_20_21=0 if `var'!=.
replace `var'_20_21=1 if `var'>=2020 & `var'<2022
gen `var'_22_23=0 if `var'!=.
replace `var'_22_23=1 if `var'>=2022 & `var'<2024
gen `var'_24_25=0 if `var'!=.
replace `var'_24_25=1 if `var'>=2024 & `var'<2026
gen `var'_26_27=0 if `var'!=.
replace `var'_26_27=1 if `var'>=2026 & `var'<2028
gen `var'_28_29=0 if `var'!=.
replace `var'_28_29=1 if `var'>=2028 & `var'<2030
gen `var'_30_31=0 if `var'!=.
replace `var'_30_31=1 if `var'>=2030 & `var'<2032
gen `var'_32_33=0 if `var'!=.
replace `var'_32_33=1 if `var'>=2032 & `var'<2034
gen `var'_34_35=0 if `var'!=.
replace `var'_34_35=1 if `var'>=2034 & `var'<2036
gen `var'_36_37=0 if `var'!=.
replace `var'_36_37=1 if `var'>=2036 & `var'<2038
gen `var'_38_40=0 if `var'!=.
replace `var'_38_40=1 if `var'>=2038 & `var'<2041

gen `var'_10levels=.
replace `var'_10levels=1 if `var'>=2020 & `var'<2022
replace `var'_10levels=2 if `var'>=2022 & `var'<2024
replace `var'_10levels=3 if `var'>=2024 & `var'<2026
replace `var'_10levels=4 if `var'>=2026 & `var'<2028
replace `var'_10levels=5 if `var'>=2028 & `var'<2030
replace `var'_10levels=6 if `var'>=2030 & `var'<2032
replace `var'_10levels=7 if `var'>=2032 & `var'<2034
replace `var'_10levels=8 if `var'>=2034 & `var'<2036
replace `var'_10levels=9 if `var'>=2036 & `var'<2038
replace `var'_10levels=10 if `var'>=2038 & `var'<2041
lab values `var'_10levels tmng_10levels_lab
}

* Conjoint choice
gen choice_cj100=choice_cj*100

label values own_cstpth_cj cstpth_lab
label values other_cstpth_cj cstpth_lab

* Buckets for adaptation vs mitigation
lab define adapt_levels_lab 1 "0-19%" 2 "20-39%" 3 "40-59%" 4 "60-79%" 5 "80-100%"
foreach var in own_adapt_cj other_adapt_cj own_mitig_cj other_mitig_cj {
su `var', det
gen `var'_0_19=0
replace `var'_0_19=1 if `var'<20
gen `var'_20_39=0
replace `var'_20_39=1 if `var'>=20 & `var'<40
gen `var'_40_59=0
replace `var'_40_59=1 if `var'>40 & `var'<60
gen `var'_60_79=0
replace `var'_60_79=1 if `var'>60 & `var'<80
gen `var'_80_100=0
replace `var'_80_100=1 if `var'>80 & `var'<=100

gen `var'_5levels=.
replace `var'_5levels=1 if `var'<=20
replace `var'_5levels=2 if `var'>20 & `var'<=40
replace `var'_5levels=3 if `var'>40 & `var'<=60
replace `var'_5levels=4 if `var'>60 & `var'<=80
replace `var'_5levels=5 if `var'>80 & `var'<=100
lab values `var'_5levels adapt_levels_lab
}

*=============================================================================

*** Saving

save "$data/us_scenariolevel_doubletiming.dta", replace
